var baseConfig = require("config.js");

import {
  tansParams
} from './util'

function request(params) {
  let config = {
    url: baseConfig.domain + params.url,
    method: params.method == undefined ? "POST" : params.method,
    timeout: 1200000,
    dataType: 'json',
    header: {
      // 'MINI-TOC': 'openId',
    }
  }

  if (wx.getStorageSync('token') && wx.getStorageSync('token') != '') {
    config.header.Authorization = 'Bearer ' + wx.getStorageSync('token')
  }

  if (params.header) {
    config.header = {
      ...config.header,
      ...params.header
    }
  }

  if (params.params) {
    config.url = config.url + '?' + tansParams(params.params)
    config.url = config.url.slice(0, -1);
  }

  if (params.data) {
    config.data = params.data
  }

  wx.request({
    ...config,
    success: function (res) {
      if (res.statusCode == 200) {
        //如果有定义了params.callBack，则调用 params.callBack(res.data)
        if (res.data.code == 0) {
          if (params.callBack) {
            params.callBack(res.data);
          }
        } else {
          if (params.errCallBack) {
            params.errCallBack(res);
          }
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          })
        }
      } else if (res.statusCode == 401) {
        wx.removeStorageSync('token')
        wx.removeStorageSync('ossAuthentication')
        if (params.url !== '/login') {
          login(params)
        } else {
          wx.showToast({
            title: '暂无权限',
            icon: 'none'
          })
        }
      } else if (res.statusCode == 424) {
        wx.showToast({
          title: '暂无权限',
          icon: "none"
        })
      } else if (res.statusCode == 403) {
        wx.showToast({
          title: "暂无权限",
          icon: "none"
        });
      } else if (res.statusCode == 400) {
        wx.showToast({
          title: res.msg,
          icon: "none"
        })
      } else if (res.statusCode == 500) {
        wx.showToast({
          title: "服务器出了点小差",
          icon: "none"
        });
      } else {
        //如果有定义了params.errCallBack，则调用 params.errCallBack(res.data)
        if (params.errCallBack) {
          params.errCallBack(res);
        }
      }
    },
    fail: function (err) {
      wx.hideLoading();
      wx.showToast({
        title: "服务器出了点小差",
        icon: "none"
      });
    }
  })
}

function login(params) {
  wx.login({
    success: (obj) => {
      request({
        url: '/login',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          code: obj.code
        },
        callBack: (res) => {
          if (res.code === 0) {
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('ossAuthentication', JSON.stringify(res.data.ossAuthentication))
            if (params) {
              request(params)
            }
          }
        }
      })
    },
  })
}

exports.request = request;
exports.login = login;